package com.ydd.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.YddCateDto;
import com.ydd.dto.YddCatePageDto;
import com.ydd.entity.YddCate;
import com.ydd.entity.YddTag;
import com.ydd.result.Result;
import com.ydd.service.YddCateService;
import com.ydd.vo.YddCateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {
    @Autowired
    private YddCateService yddCateService;

    /**
     * 分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResponseEntity<Map<String, Object>> getCateById(@RequestParam("id") Long id) {
        YddCateDto yddCateDto = yddCateService.getCateById(id);

        Map<String, Object> responseData = new HashMap();

        responseData.put("id", yddCateDto.getId());
        responseData.put("cateName", yddCateDto.getCatename());
        responseData.put("sortNum", yddCateDto.getSortnum());
        responseData.put("icon", yddCateDto.getIcon());

        return ResponseEntity.ok(responseData);
    }

    /**
     * 根据id删除标签
     *
     * @param id
     * @return 返回删除结果
     */
    @GetMapping("/del")
    public Result<Map<String, Object>> deleteById(@RequestParam("id") Long id) {
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("result", yddCateService.deleteById(id));
        return Result.success(responseData);
    }

    /**
     * 根据id修改标签内容
     *
     * @param yddCate
     * @return 返回标签内容
     */
    @PostMapping("/update")
    public Result<YddCate> updateCate(@Valid @RequestBody YddCate yddCate) {
        if (yddCateService.updateCate(yddCate)) {
            return Result.success(yddCate);
        } else {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
    }

    /**
     * 添加分类
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public Result<YddTag> addCate(@RequestBody YddCate yddCate) {
        boolean success = yddCateService.addCate(yddCate);
        if (!success) {
            return Result.error("添加分类失败");
        }
        return Result.success();
    }

    /**
     * 查询分类列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public Result<List<YddCate>> selectCateList() {
        List<YddCate> cateList = yddCateService.selectCateList();
        return Result.success(cateList);
    }

    /**
     * 分类管理分页
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Result YddCateList(YddCatePageDto dto){
        return yddCateService.YddCateList(dto);
    }
}
